package com.rrtv.rpc.client.transport;

import com.rrtv.rpc.core.common.ServiceInfo;
import com.rrtv.rpc.core.common.ServiceUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: changjiu.wang
 * @Date: 2021/7/25 15:12
 */
@Slf4j
public class NetClientTransportFactory {
    private static Map<String, NetClientTransport> map = new HashMap<>();

    public static NetClientTransport getNetClientTransport(ServiceInfo serviceInfo) {
        String key = ServiceUtil.serviceKey(serviceInfo.getAddress(), serviceInfo.getPort());
        if (map.containsKey(key)) {
            return map.get(key);
        }
        synchronized (NetClientTransportFactory.class) {
            if (!map.containsKey(key)) {
                map.put(key, new NettyNetClientTransport(serviceInfo));
            }
            return map.get(key);
        }
    }
}
